package com.fp.m7.dao;

import java.util.List;

import com.fp.m7.model.Account;

public class AccountDaoImpl extends BaseDaoImpl<Account> implements AccountDao {

    public AccountDaoImpl() {
        super(Account.class);
    }

    @Override
    public Account findByUsername(String username) {
        String query = "from Account a where a.username = :username";
        List<Account> list = em.createQuery(query, Account.class) //
                .setParameter("username", username) //
                .getResultList();

        return list.isEmpty() ? null : list.get(0);
    }
    
    @Override
    public Account findByNickname(String nickname) {
        String query = "from Account u where u.nickname = :nickname";
        List<Account> found = em.createQuery(query, Account.class) //
                .setParameter("nickname", nickname) //
                .getResultList();

        return found.isEmpty() ? null : found.get(0);
    }

    @Override
    public List<Account> findRandom(int count) {
        String query = "from Account u order by RAND()";
        List<Account> found = em.createQuery(query, Account.class) //
                .setMaxResults(count) //
                .getResultList();

        return found;
    }

}
